package com.chanven.lib.cptr;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;

import com.chanven.lib.cptr.header.HeaderChangeInterface;

public class PtrClassicFrameLayout extends PtrFrameLayout {

    private String TAG = getClass().getSimpleName();

    private PtrClassicDefaultHeader mPtrClassicHeader;

    private boolean isRefreshing = true;

    private Handler handler = new Handler();

    public PtrClassicFrameLayout(Context context) {
        super(context);
        initViews();
    }

    public PtrClassicFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public PtrClassicFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }

    private void initViews() {
        mPtrClassicHeader = new PtrClassicDefaultHeader(getContext());
        setHeaderView(mPtrClassicHeader);
        addPtrUIHandler(mPtrClassicHeader);
        setDurationToClose(100);
        setDurationToCloseHeader(600);
        mPtrClassicHeader.setHeaderChangeInterface(new HeaderChangeInterface() {
            @Override
            public void headerChange(int index, int maxIndex) {
//                Log.e(TAG,"-------->INDEX="+index+":"+maxIndex);
//                if(index == 0 && !isCanTouchEvent()){
//                    setCanTouchEvent(true);
//                }
                if(null != headerChangeInterface){
                    headerChangeInterface.headerChange(index,maxIndex);
                }
            }

            @Override
            public void onUIRefreshBegin() {
 //               Log.e(TAG,"-------->onUIRefreshBegin");
                isRefreshing = true;
                if(null != headerChangeInterface){
                    headerChangeInterface.onUIRefreshBegin();
                }
            }

            @Override
            public void onUIRefreshComplete() {
 //               Log.e(TAG,"------->onUIRefreshComplete");
                if(isRefreshing){
                    isRefreshing = false;
                    setCanTouchEvent(false);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setCanTouchEvent(true);
                            resetHead();
                        }
                    },620);
                    return;
                }
                if(null != headerChangeInterface){
                    headerChangeInterface.onUIRefreshComplete();
                }
            }

            @Override
            public void onUIReset() {
                setCanTouchEvent(true);
                if(null != headerChangeInterface){
                    headerChangeInterface.onUIReset();
                }
            }
        });
    }

    public PtrClassicDefaultHeader getHeader() {
        return mPtrClassicHeader;
    }

    /**
     * Specify the last update time by this key string
     *
     * @param key
     */
    public void setLastUpdateTimeKey(String key) {
        if (mPtrClassicHeader != null) {
            mPtrClassicHeader.setLastUpdateTimeKey(key);
        }
    }

    /**
     * Using an object to specify the last update time.
     *
     * @param object
     */
    public void setLastUpdateTimeRelateObject(Object object) {
        if (mPtrClassicHeader != null) {
            mPtrClassicHeader.setLastUpdateTimeRelateObject(object);
        }
    }

    public void onBeforeUIRefreshComplete(){
        if(mPtrClassicHeader != null){
            mPtrClassicHeader.onBeforeUIRefreshComplete();
        }
    }

    public void setWhiteTheme(boolean is){
        if(null != mPtrClassicHeader){
            mPtrClassicHeader.setWhiteTheme(is);
        }
    }
}
